﻿// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Resources;
using NUnit.Framework;

namespace Azure.ResourceManager.TrafficManager.Tests
{
    public sealed class HeatMapModelTests : TrafficManagerManagementTestBase
    {
        private const string ProfileWithHeatmapResoruceGroupName = "dialtone-traffic-manager";
        private const string ProfileWithHeatmapName = "az-int-int-msftmetrics";

        private SubscriptionResource _subscription;

        public HeatMapModelTests(bool isAsync) : base(isAsync) //, RecordedTestMode.Record)
        { }

        private TrafficManagerHeatMapResource DefaultHeatmapModelResource =>
            new TrafficManagerHeatMapResource(
                Client,
                TrafficManagerHeatMapResource.CreateResourceIdentifier(
                    _subscription.Data.SubscriptionId,
                    ProfileWithHeatmapResoruceGroupName,
                    ProfileWithHeatmapName,
                    Models.TrafficManagerHeatMapType.Default));

        private TrafficManagerHeatMapCollection DefaultHealmapModelCollection =>
            new TrafficManagerHeatMapCollection(
                Client,
                TrafficManagerProfileResource.CreateResourceIdentifier(
                    _subscription.Data.SubscriptionId,
                    ProfileWithHeatmapResoruceGroupName,
                    ProfileWithHeatmapName));

        /// <summary>
        /// The method creates a traffic manager profile, so it covers the creation use-case.
        /// </summary>
        /// <returns>A task to wait for.</returns>
        [SetUp]
        public async Task Setup()
        {
            _subscription = await Client.GetDefaultSubscriptionAsync();
        }

        [PlaybackOnlyAttribute("Hard to generate test data for execution in RecordedTestMode.Live. The test data here is a data generated by real users.")]
        [RecordedTest]
        public async Task GetTest()
        {
            TrafficManagerHeatMapResource heatMapModelResource = await DefaultHeatmapModelResource.GetAsync();

            Assert.IsNotNull(heatMapModelResource);
            Assert.IsTrue(heatMapModelResource.HasData);
            Assert.IsNotNull(heatMapModelResource.Data);
            Assert.IsTrue(heatMapModelResource.Data.TrafficFlows.Count > 0);
        }

        [PlaybackOnlyAttribute("Hard to generate test data for execution in RecordedTestMode.Live. The test data here is a data generated by real users.")]
        [RecordedTest]
        public async Task GetWithCoordinatesConstraintTest()
        {
            // Rio de Janeiro and San Paulo area
            TrafficManagerHeatMapResource heatMapModelResource =
                await DefaultHeatmapModelResource.GetAsync(
                    topLeft: new List<double> { -18.910742, -47.858517 },
                    botRight: new List<double> { -24.341378, -37.964283 });

            Assert.IsNotNull(heatMapModelResource);
            Assert.IsTrue(heatMapModelResource.HasData);
            Assert.IsNotNull(heatMapModelResource.Data);
            Assert.IsTrue(heatMapModelResource.Data.TrafficFlows.Count > 0);

            // Mariupol
            heatMapModelResource =
                await DefaultHeatmapModelResource.GetAsync(
                    topLeft: new List<double> { 47.198208, 37.385117 },
                    botRight: new List<double> { 47.072374, 37.707283 });

            Assert.IsNotNull(heatMapModelResource);
            Assert.IsTrue(heatMapModelResource.HasData);
            Assert.IsNotNull(heatMapModelResource.Data);
            Assert.AreEqual(0, heatMapModelResource.Data.TrafficFlows.Count);
        }

        [PlaybackOnlyAttribute("Hard to generate test data for execution in RecordedTestMode.Live. The test data here is a data generated by real users.")]
        [RecordedTest]
        public async Task GetFromCollectionTest()
        {
            TrafficManagerHeatMapResource heatMapModelResource =
                await DefaultHealmapModelCollection.GetAsync(Models.TrafficManagerHeatMapType.Default);

            Assert.IsNotNull(heatMapModelResource);
            Assert.IsTrue(heatMapModelResource.HasData);
            Assert.IsNotNull(heatMapModelResource.Data);
            Assert.IsTrue(heatMapModelResource.Data.TrafficFlows.Count > 0);
        }

        [PlaybackOnlyAttribute("Hard to generate test data for execution in RecordedTestMode.Live. The test data here is a data generated by real users.")]
        [RecordedTest]
        public async Task ExistsTest()
        {
            Assert.IsTrue(await DefaultHealmapModelCollection.ExistsAsync(Models.TrafficManagerHeatMapType.Default));
        }
    }
}
